package controllers;

import play.data.DynamicForm;
import play.data.Form;
import play.mvc.*;
import views.html.*;
import models.*;

public class Application extends Controller {

    public static Result index() {
    	String userId = session("usrId");
    	String userName = session("usrName");
    	String helloString = null;
    	if(userId != null && userName != null)
    		helloString = userName;
        return ok(index.render(helloString,Tables.getNonClonedTables()));
    }
    
    public static Result cardSet1() {
    	return ok(views.html.cardhand.render("1","2","4","8","16"));
    	//return ok("1;2;4;8;16");
    }
    
    public static Result newTable() {
    	String userID = session("usrId");
    	if(userID == null) {
    		String address = routes.Application.newTable().absoluteURL(request());
    		session("goAfter", address);
    		return redirect(routes.Application.newUser());
    	}
    	return ok(views.html.newtable.render());
    }
    
    public static Result createTable() {
    	DynamicForm requestData = Form.form().bindFromRequest();
    	String tableName = requestData.get("tableName");
    	int humanPlayers, computerPlayers, tokensCount, entranceFee;
    	try {
    		humanPlayers = Integer.parseInt(requestData.get("humanPlayers"));
    		computerPlayers = Integer.parseInt(requestData.get("computerPlayers"));
    		tokensCount = Integer.parseInt(requestData.get("tokensCount"));
    		entranceFee = Integer.parseInt(requestData.get("entranceFee"));
    	} catch(NumberFormatException e) {
    		return badRequest(views.html.badreq.render(e.getMessage()));
    	}
    	//Table t = new Table(tableName, humanPlayers, computerPlayers, tokensCount, 
    	//		entranceFee, humanPlayers - 1);
    	PlayTable t = new PlayTable(tableName, 0, 0, humanPlayers, computerPlayers, tokensCount, entranceFee);
    	long id = Tables.addTable(t);
    	t.setTableId(id);
    	return joinTable(id);
    }
    
    public static Result newUser() {
    	return ok(views.html.newuser.render());
    }
    
    public static Result showMessageSite(String message, String returnLink) {
    	return ok(views.html.textmsg.render(message, returnLink));
    }
    
    public static Result messageSiteWithLinkToIndex(String message) {
    	return ok(views.html.textmsg.render(message, routes.Application.index().absoluteURL(request())));
    }
    
    public static Result createUser() {
    	DynamicForm requestData = Form.form().bindFromRequest();
    	String playerName = requestData.get("userName");
    	if(playerName == null || playerName.isEmpty() || Players.playerWithSameNameExists(playerName)) return messageSiteWithLinkToIndex("Nie podano nazwy uzytkownika.");
    	long pId = Players.addPlayer(playerName);
    	if(pId == 0) return messageSiteWithLinkToIndex("Nie udalo sie utworzyc uzytkownika");
    	session("usrId", Long.toString(pId));
    	session("usrName", playerName);
    	String goAfter = session("goAfter");
    	if(goAfter == null) return redirect(routes.Application.index());
    	else return redirect(goAfter);
    }
    
    public static Result joinTable(long id) {
    	String userID = session("usrId");
    	if(userID == null) {
    		String address = routes.Application.index().absoluteURL(request()) + "table/askForJoin/" + id;
    		session("goAfter", address);
    		return redirect(routes.Application.newUser());
    	}
    	return redirect(routes.PokerPlay.askForJoin(id));
    }
}
